﻿$(function () {
            var id = querySt('id');
            $('#txtStartsAt').datetimepicker();
            $('#txtEndsAt').datetimepicker();
            if (id != "") {
                $('#btnSave').html('Update event');
                var data = '{"id":"' + id + '"}';
                ajaxCall('../DataService.asmx/GetEventById', false, data,
                    function (msg) {
                        $('#txtEventName').val(msg.d.EventName);
                        $('#txtStartsAt').val(JSONDateToNormal(msg.d.StartingFrom) + ' ' + JSONTime(msg.d.StartingFrom));
                        $('#txtEndsAt').val(JSONDateToNormal(msg.d.EndingAt) + ' ' + JSONTime(msg.d.EndingAt));
                        $('#txtLocation').val(msg.d.Location);
                        $("#lat").val(msg.d.Latitude);
                        $('#lng').val(msg.d.Longitude);
                        var map;
                        var geocoder;

                        if (GBrowserIsCompatible()) {
                            map = new GMap2(document.getElementById("map"));
                            map.addControl(new GSmallMapControl());
                            map.addControl(new GMapTypeControl());
                            map.setCenter(new GLatLng(41.99593, 21.43145), 15);
                            map.setUIToDefault();
                            var centar = new GLatLng(msg.d.Latitude, msg.d.Longitude);
                            var marker = new GMarker(centar, { draggable: true });
                            map.setCenter(centar);
                            GEvent.addListener(marker, "click", function () {
                                marker.openInfoWindow(msg.d.Location);
                            });
                            map.addOverlay(marker);
                            marker.openInfoWindow(msg.d.Location);


                            GEvent.addListener(marker, "dragend", function () {
                                var point = marker.getPoint();
                                map.panTo(point);
                                $("#lat").val(point.lat().toFixed(5));
                                $("#lng").val(point.lng().toFixed(5));

                            });
                            GEvent.addListener(map, "moveend", function () {
                                map.clearOverlays();

                                var center = map.getCenter();
                                var marker = new GMarker(center, { draggable: true });
                                map.addOverlay(marker);
                                $("#lat").val(center.lat().toFixed(5));
                                $("#lng").val(center.lng().toFixed(5));
                                GEvent.addListener(marker, "dragend", function () {
                                    var point = marker.getPoint();
                                    map.panTo(point);
                                    $("#lat").val(point.lat().toFixed(5));
                                    $("#lng").val(point.lng().toFixed(5));

                                });

                            });
                        }
                    },
                    function () {
                        alert("Error happend while reading data from database");
                    }
                );
                
            } 
            else {
                var map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl());
                map.addControl(new GMapTypeControl());
                var center = new GLatLng(41.99318, 21.43021);
                map.setCenter(center, 15);
                geocoder = new GClientGeocoder();
                var marker = new GMarker(center, { draggable: true });
                map.addOverlay(marker);
                $("#lat").val(center.lat().toFixed(5));
                $("#lng").val(center.lng().toFixed(5));

                if (GBrowserIsCompatible()) {
                    GEvent.addListener(marker, "dragend", function () {
                        var point = marker.getPoint();
                        map.panTo(point);
                        debugger;
                        $("#lat").val(point.lat().toFixed(5));
                        $("#lng").val(point.lng().toFixed(5));

                    });

                    GEvent.addListener(map, "moveend", function () {
                        map.clearOverlays();
                        var center = map.getCenter();
                        var marker = new GMarker(center, { draggable: true });
                        map.addOverlay(marker);
                        $("#lat").val(center.lat().toFixed(5));
                        $("#lng").val(center.lng().toFixed(5));
                        GEvent.addListener(marker, "dragend", function () {
                            var point = marker.getPoint();
                            map.panTo(point);
                            $("#lat").val(point.lat().toFixed(5));
                            $("#lng").val(point.lng().toFixed(5));

                        });

                    });
                }
            }


            $('#btnSave').click(function () {
                $('#createEventValidation').validate({
                    rules: {
                        eventname: "required",
                        startsat: "required",
                        endsat: "required",
                        location: "required"
                        
                    },
                    messages: {
                        eventname: "Event name is required",
                        startsat: "When the event starts",
                        endsat: "When the event ends",
                        location: "Location is required"
                        
                    }
                 });

                if ($('#createEventValidation').valid()) {
                    var dataToSend;
                    if (id == "") {
                        dataToSend = '{"eventName":"' + $('#txtEventName').val() + '", "startingFrom":"' + $('#txtStartsAt').val() + '", "endingAt":"' + $('#txtEndsAt').val() + '", "location":"' + $('#txtLocation').val() + '", "latitude":"' + $('#lat').val() + '", "longitude":"' + $('#lng').val() + '"}';
                        ajaxCall('../DataService.asmx/InsertEvent', false, dataToSend,
                            function () {
                                $('#txtEventName').val("");
                                $('#txtStartsAt').val("");
                                $('#txtEndsAt').val("");
                                $('#txtLocation').val("");
                            },
                            function () {
                                 alert("Error happend while insertign data to database");
                            }
                        );
                       
                    } else {
                        dataToSend = '{"eventId":"' + id + '", "eventName":"' + $('#txtEventName').val() + '", "startingFrom":"' + $('#txtStartsAt').val() + '", "endingAt":"' + $('#txtEndsAt').val() + '", "location":"' + $('#txtLocation').val() + '", "latitude":"' + $('#lat').val() + '", "longitude":"' + $('#lng').val() + '"}';
                        ajaxCall('../DataService.asmx/UpdateEvent', false, dataToSend,                        
                            function () {
                                alert("Update was successful");
                                window.location = "default.aspx";
                            },
                            function () {
                                 alert("Error happend while updating data from database");
                            }
                        );
                      }
                }
            });
        });